home *** CD-ROM | disk | FTP | other *** search
- /* © 1988-91, Bowers Development Corp. */
- /* EventLoop.c */
-
- #include <Types.h>
- #include <Quickdraw.h>
- #include <Controls.h>
- #include <Desk.h>
- #include <Dialogs.h>
- #include <DiskInit.h>
- #include <Editions.h>
- #include <EPPC.h>
- #include <Events.h>
- #include <Fonts.h>
- #include <GestaltEqu.h>
- #include <Lists.h>
- #include <Menus.h>
- #include <OSEvents.h>
- #include <TextEdit.h>
- #include <ToolUtils.h>
- #include <Traps.h>
- #include <AppleEvents.h>
-
- #include "Globals.h"
- #include "Cursors.h"
- #include "DoScrap.h"
- #include "Miscellany.h"
- #include "WindowAids.h"
- #include "Windowing.h"
- #include "Dispatcher.h"
- #include "MainMenu.h"
- #include "AEvent.h"
-
- #include "EventLoop.h"
-
- /*----------*/
- void DoMouseDown (void);
- void DoMouseUp (void);
- void DoKeystroke (void);
- void DoActivate (void);
-
- /*----------*/
- #if defined(applec) /* MPW C */
- extern void _DataInit (void);
- #endif /* MPW C */
-
- /*----------*/
- Boolean TrapAvailable (short tNumber,
- TrapType tType);
- Boolean TrapAvailable (short tNumber,
- TrapType tType)
- {
- return (NGetTrapAddress (tNumber, tType)
- != GetTrapAddress (_Unimplemented));
- } /*TrapAvailable*/
-
- #if defined(applec) /* MPW C */ || defined(THINK_C)
- /*----------*/
- static void SetStackSize (long stackSize);
- static void SetStackSize (long stackSize)
- {
- SetApplLimit ((Ptr) ((long) &stackSize - stackSize));
- } /*SetStackSize*/
- #endif /* MPW C or Think C */
-
- /*----------*/
- static void GetSysConfig (void);
- static void GetSysConfig (void)
- {
- OSErr ignoreError;
- long tempLong; /*for Gestalt*/
- SysEnvRec environs;
-
- sysConfig.hasGestalt = (Gestalt (gestaltVersion, &tempLong) == noErr);
- if (sysConfig.hasGestalt) {
- sysConfig.hasWNE = TrapAvailable (_WaitNextEvent, ToolTrap);
- ignoreError = Gestalt (gestaltQuickdrawVersion, &tempLong);
- sysConfig.hasColorQD = (tempLong != gestaltOriginalQD);
- sysConfig.hasAppleEvents = (Gestalt (gestaltAppleEventsAttr, &tempLong) == noErr);
- sysConfig.hasEditionMgr = (Gestalt (gestaltEditionMgrAttr, &tempLong) == noErr);
- if (sysConfig.hasEditionMgr) {
- if (!CheckOS (InitEditionPack ())) {
- sysConfig.hasEditionMgr = false; /*unable to load Edition Manager package*/
- }
- }
- } else {
- ignoreError = SysEnvirons (curSysEnvVers, &environs);
- if (environs.machineType < 0) {
- sysConfig.hasWNE = false;
- } else {
- sysConfig.hasWNE = TrapAvailable (_WaitNextEvent, ToolTrap);
- }
- sysConfig.hasColorQD = environs.hasColorQD;
- sysConfig.hasAppleEvents = false;
- sysConfig.hasEditionMgr = false;
- }
- } /*GetSysConfig*/
-
- /*----------*/
- void Initialize ()
- {
-
- #if defined(applec) /* MPW C */
- UnloadSeg ((Ptr) _DataInit);
- #endif /* MPW C */
-
- #if defined(applec) /* MPW C */ || defined(THINK_C)
- SetStackSize (15000);
- #endif /* MPW C or Think C */
-
- MaxApplZone ();
- MoreMasters ();
-
- InitGraf (&qd.thePort);
- InitFonts ();
- InitWindows ();
- InitMenus ();
- TEInit ();
- InitDialogs (NULL);
-
- FlushEvents (everyEvent - diskMask - app4Mask, 0);
-
- GetSysConfig ();
- if (sysConfig.hasAppleEvents) {
- InitializeAE ();
- }
- inBackground = false;
-
- LoadMenus ();
- LoadCursors ();
- InitScrap ();
-
- InitGlobals ();
-
- InitModelessDialogs ();
- InitTitles (); /*initialize each menu title's module*/
- } /*Initialize*/
-
- /*----------*/
- void DoMouseDown ()
- {
- WindowPtr whichWindow;
- short whichPart;
-
- whichPart = FindWindow (curEvent.where, &whichWindow);
- switch (whichPart) {
- case inDesk:
- /*Do nothing*/;
- break;
- case inMenuBar:
- DoMenu (MenuSelect (curEvent.where));
- break;
- case inSysWindow:
- SystemClick (&curEvent, whichWindow);
- break;
- case inContent:
- DoContent (whichWindow);
- break;
- case inDrag:
- DoDrag (whichWindow);
- break;
- case inGrow:
- DoGrow (whichWindow);
- break;
- case inGoAway:
- DoGoAway (whichWindow);
- break;
- case inZoomIn:
- DoZoom (whichWindow, inZoomIn);
- break;
- case inZoomOut:
- DoZoom (whichWindow, inZoomOut);
- break;
- } /*switch*/
- } /*DoMouseDown*/
-
- /*----------*/
- void DoMouseUp ()
- {
- WindowPtr whichWindow;
- short whichPart;
-
- whichPart = FindWindow (curEvent.where, &whichWindow);
- /*save event time to check later for double click*/
- } /*DoMouseUp*/
-
- /*----------*/
- void DoKeystroke ()
- {
- short charCode;
- char ch;
-
- charCode = curEvent.message & charCodeMask;
- ch = charCode;
-
- if ((curEvent.modifiers & cmdKey) != 0) {
- if (curEvent.what != autoKey) {
- DoMenu (MenuKey (ch));
- }
- } else {
- if (curWindow == NULL) {
- SysBeep (1);
- } else {
- TypeInWindow (ch);
- }
- }
- } /*DoKeystroke*/
-
- /*----------*/
- void DoUpdate ()
- {
- GrafPtr savePort;
- WindowPtr saveWindow;
- WindowPtr whichWindow;
-
- GetPort (&savePort);
- whichWindow = (WindowPtr) curEvent.message;
- SetPort (whichWindow);
- saveWindow = curWindow;
- SetInfo (whichWindow);
- BeginUpdate (whichWindow);
- EraseRect (&(whichWindow->portRect));
- UpdateContent ();
- DrawControls (whichWindow);
- EndUpdate (whichWindow);
- SetInfo (saveWindow);
- SetPort (savePort);
- } /*DoUpdate*/
-
- /*----------*/
- void DoActivate ()
- {
- Boolean activate;
- WindowPtr whichWindow;
-
- whichWindow = (WindowPtr) curEvent.message;
- SetPort (whichWindow);
- SetInfo (whichWindow);
-
- activate = ((curEvent.modifiers & activeFlag) != 0);
- if (activate) {
- if ((cur->text) != NULL) {
- TEActivate (cur->text);
- }
- ReadDeskScrap ();
- }
-
- HiliteScroll (cur->vScroll, activate);
- HiliteScroll (cur->hScroll, activate);
-
- ActivateContent (activate);
-
- if (!activate) {
- WriteDeskScrap ();
- if (cur->text != NULL) {
- TEDeactivate (cur->text);
- }
- SetInfo (NULL);
- }
- } /*DoActivate*/
-
- /*----------*/
- void DoDiskEvent ()
- {
- #define dlgTop 75
- #define dlgLeft 100
-
- OSErr ignore;
- Point where;
-
- if (HiWord (curEvent.message) != noErr) {
- DILoad ();
- SetPt (&where, dlgLeft, dlgTop);
- ignore = DIBadMount (where, curEvent.message);
- DIUnload ();
- }
- } /*DoDiskEvent*/
-
- /*----------*/
- void DoApp4Event ()
- {
- #define suspendResumeMsg 1
- #define resumeMask 1
-
- Boolean resume;
- WindowPtr front;
-
- curEvent.what = nullEvent;
- if (BitShift (curEvent.message, -24) == suspendResumeMsg) {
- resume = ((curEvent.message & resumeMask) != 0);
- if (resume) {
- ReadDeskScrap ();
- } else {
- WriteDeskScrap ();
- }
- inBackground = !resume;
- front = FrontWindow ();
- if (front != NULL) {
- curEvent.what = activateEvt;
- curEvent.modifiers = LoWord (curEvent.message);
- curEvent.message = (long) front;
- }
- }
- } /*DoApp4Event*/
-
- /*----------*/
- static long GetSleep (void);
- static long GetSleep (void)
- {
- long sleep;
-
- sleep = GetCaretTime (); /* if long time, some Inits won't run */
- if ((!inBackground) && (FrontWindow () != NULL)) {
- if (FrontWindow () == curWindow) {
- if (cur->text != NULL) {
- if ((**(cur->text)).selStart == (**(cur->text)).selEnd) {
- sleep = GetCaretTime ();
- }
- }
- } else { /* DA or modeless dialog */
- sleep = GetCaretTime ();
- }
- }
- return (sleep);
- } /*GetSleep*/
-
- /*----------*/
- static Boolean GetEvent (void);
- static Boolean GetEvent (void)
- {
- Boolean gotEvent;
-
- if (sysConfig.hasWNE) {
- gotEvent = WaitNextEvent (everyEvent, &curEvent,
- GetSleep (), cursorRgn);
- } else {
- SystemTask ();
- gotEvent = GetNextEvent (everyEvent, &curEvent);
- }
- return (gotEvent);
- } /*GetEvent*/
-
- /*----------*/
- static Boolean DoEvent (void);
- static Boolean DoEvent (void)
- {
- Boolean gotEvent;
- DialogPtr whichDialog;
- short itemHit;
-
- gotEvent = GetEvent ();
- if (curEvent.what == app4Evt) {
- /* here so event won't get swallowed by IsDialogEvent */
- /* and so suspend/resume can be translated to activate */
- DoApp4Event ();
- }
- if (IsDialogEvent (&curEvent)) {
- if ((curEvent.what == activateEvt)
- || (curEvent.what == updateEvt)) {
- whichDialog = (DialogPtr) curEvent.message;
- } else {
- whichDialog = FrontWindow ();
- }
- SetPort (whichDialog);
- if ((Boolean) FilterModeless (whichDialog, &curEvent, &itemHit)) {
- DoModelessItem (whichDialog, itemHit);
- } else if ((curEvent.what == keyDown)
- && ((curEvent.modifiers & cmdKey) != 0)) {
- DoKeystroke (); /* => calls MenuKey */
- } else if (DialogSelect (&curEvent, &whichDialog, &itemHit)) {
- DoModelessItem (whichDialog, itemHit);
- }
- } else if (gotEvent) {
- switch (curEvent.what) {
- case mouseDown:
- DoMouseDown ();
- break;
- case mouseUp:
- DoMouseUp ();
- break;
- case keyDown:
- case autoKey:
- DoKeystroke ();
- break;
- case updateEvt:
- DoUpdate ();
- break;
- case activateEvt:
- DoActivate ();
- break;
- case diskEvt:
- DoDiskEvent ();
- break;
- case kHighLevelEvent:
- if (sysConfig.hasAppleEvents) {
- DoHighLevelEvent ();
- }
- break;
- default:
- DoIdle ();
- break;
- } /*switch*/
- } else { /*nullEvent*/
- DoIdle ();
- }
- return (gotEvent);
- } /*DoEvent*/
-
- /*----------*/
- void MainLoop ()
- {
- UpdateMenus ();
-
- quittingTime = false;
- while (!quittingTime) {
- ShapeCursor ();
- if (cur->text != NULL) {
- TEIdle (cur->text);
- }
- if (DoEvent ()) {
- UpdateMenus ();
- }
- } /*while*/
-
- WriteDeskScrap ();
- } /*MainLoop*/
-